<?php

/**
 * 评论自定义字段
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class FieldAction extends AdminbaseAction {
    
    public $CommentsSetting;
    
    function _initialize() {
        parent::_initialize();
        $this->CommentsSetting = F("Comments_setting");
        if(empty($this->CommentsSetting)){
            $this->error("初始化错误，请更新缓存！");
        }
    }

    public function index() {
        $data = M("CommentsField")->order(array("fid" => "DESC"))->select();
        $this->assign("data", $data);
        $this->display();
    }

    /**
     * 添加字段 
     */
    public function add() {
        if ($this->isPost()) {
            if (!$this->checkfield($this->_post("f"))) {
                $this->error("该字段已经存在！");
            }
            $db = M("CommentsField");
            if ($data = $db->create()) {
                //字段类型
                $field = $this->ReturnPlFtype($data);
                $stbsum = $this->CommentsSetting;
                
                if (empty($field) || empty($stbsum)) {
                    $this->error("初始化信息出错！");
                }
                $data['regular'] = Input::forTag($data['regular']);
                if ($db->add($data)) {
                    $Model = new Model();
                    if ($this->_post("issystem") == "1") {
                        $Model->query("alter table " . C("DB_PREFIX") . "comments add " . $field);
                    } else {
                        for ($i = 1; $i <= $stbsum['stbsum']; $i++) {
                            $Model->query("alter table " . C("DB_PREFIX") . "comments_data_" . $i . " add " . $field);
                        }
                    }
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($db->getError());
            }
        } else {
            $this->display();
        }
    }

    /**
     * 删除字段 
     */
    public function delete() {
        $fid = $this->_get("fid");
        if (empty($fid)) {
            $this->error("参数错误！");
        }
        $db = M("CommentsField");
        $r = $db->where(array("fid" => $fid))->find();
        if (!$r) {
            $this->error("该自定义字段不存在！");
        }
        $Model = new Model();
        $stbsum = $this->CommentsSetting;
        if ($r['issystem'] == 1) {
            $Model->query("alter table " . C("DB_PREFIX") . "comments DROP `" . $r['f'] . "`");
        } else {
            for ($i = 1; $i <= $stbsum['stbsum']; $i++) {
                $Model->query("alter table " . C("DB_PREFIX") . "comments_data_" . $i . " DROP `" . $r['f'] . "` ");
            }
        }
        $db->where(array("fid" => $fid))->delete();
        $this->success("自定义字段删除成功！");
    }

    /**
     * 编辑字段 
     */
    public function edit() {
        if ($this->isPost()) {
            $db = M("CommentsField");
            $fid = (int) $this->_post("fid");
            $r = $db->where(array("fid" => $fid))->find();
            if (!$r) {
                $this->error("该自定义字段不存在！");
            }
            if ($data = $db->create()) {
                unset($data['issystem']);
                $field = $this->ReturnPlFtype($data);
                $data['regular'] = Input::forTag($data['regular']);
                if (!$db->save($data)) {
                    $this->error("更新失败！");
                }
                if ($data['f'] != $r['f']) {
                    if (!$this->checkfield($this->_post("f"))) {
                        $this->error("该字段已经存在！");
                    }
                }
                $stbsum = $this->CommentsSetting;
                if ($r['ftype'] != $data['ftype']) {
                    $Model = new Model();
                    if ($r['issystem'] == 1) {
                        $Model->query("alter table " . C("DB_PREFIX") . "comments change `" . $r['f'] . "` " . $field);
                    } else {
                        for ($i = 1; $i <= $stbsum['stbsum']; $i++) {
                            $Model->query("alter table " . C("DB_PREFIX") . "comments_data_" . $i . " change `" . $r['f'] . "` " . $field);
                        }
                    }
                }
                $this->success("更新成功！", U("Comments/Field/index"));
            } else {
                $this->error($db->getError());
            }
        } else {
            $fid = (int) $this->_get("fid");
            $data = M("CommentsField")->where(array("fid" => $fid))->find();
            if (!$data) {
                $this->error("该自定义字段不存在！");
            }
            $data['regular'] = Input::forTag($data['regular']);
            $this->assign("data", $data);
            $this->display();
        }
    }

    //返回字段类型
    protected function ReturnPlFtype($add) {
        //字段类型
        if ($add[ftype] == "TINYINT" || $add[ftype] == "SMALLINT" || $add[ftype] == "INT" || $add[ftype] == "BIGINT" || $add[ftype] == "FLOAT" || $add[ftype] == "DOUBLE") {
            $def = " default '0'";
        } elseif ($add[ftype] == "VARCHAR") {
            $def = " default ''";
        } else {
            $def = "";
        }
        $type = $add[ftype];
        //VARCHAR
        if ($add[ftype] == 'VARCHAR' && empty($add[flen])) {
            $add[flen] = '255';
        }
        //字段长度
        if ($add[flen]) {
            if ($add[ftype] != "TEXT" && $add[ftype] != "MEDIUMTEXT" && $add[ftype] != "LONGTEXT") {
                $type.="(" . $add[flen] . ")";
            }
        }
        $field = "`" . $add[f] . "` " . $type . " NOT NULL" . $def;
        return $field;
    }

    /**
     * 检查字段是否存在
     */
    public function public_checkfield() {
        $f = $this->_get("f");
        if (empty($f)) {
            $this->error("字段名称不能为空！");
        }
        if (!$this->checkfield($f)) {
            $this->error("该字段已经存在！");
        } else {
            $this->success("该字段可以正常使用！");
        }
    }

    protected function checkfield($field) {
        if (empty($field)) {
            return true;
        }
        $db = M("CommentsField");
        $count = $db->where(array("f" => $field))->count();
        if ($count > 0) {
            return false;
        } else {
            return true;
        }
    }

}

?>
